package com.yxhl.zoume.core.car.presenter;

import android.text.TextUtils;

import com.yxhl.zoume.common.info.BaseAlphabetEntity;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.car.view.common.AlphabetSearchView;
import com.yxhl.zoume.utils.alphabet.AlphabetHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.subscriptions.Subscriptions;

/**
 * Created by sugarya on 16/8/16.
 * 带有字母控件,查询Presenter 可复用
 */
public class AlphabetSearchPresenter<T extends BaseAlphabetEntity> implements IPresenter {

    private static final String TAG = "AlphabetSearchPresenter";


    private AlphabetSearchView<T> mAlphabetSearchView;
    /**
     * 列表数据集
     */
    private List<T> mSearchEntityList = new ArrayList<>();

    /**
     * 字母表数据集
     */
    private Map<String, List<T>> mAlphabetMap;
    private Subscription subscription = Subscriptions.empty();


    @Inject
    public AlphabetSearchPresenter() {
    }

    /**
     * 数据源
     *
     * @param entityList
     */
    public void setSearchEntityList(List<T> entityList) {
        if (entityList != null && entityList.size() > 0) {
            mSearchEntityList.addAll(entityList);
        }
    }

    public void setAlphabetMap(Map<String, List<T>> alphabetMap) {
        this.mAlphabetMap = alphabetMap;
    }


    /**
     * 查询
     *
     * @param searchContent 待搜索的内容
     */
    public void search(final String searchContent) {
        if (TextUtils.isEmpty(searchContent)) {
            return;
        }

        Action1<List<T>> action1 = new Action1<List<T>>() {
            @Override
            public void call(List<T> ts) {
                if (mAlphabetSearchView != null) {
                    mAlphabetSearchView.renderAlphabetSearchResult(ts);
                }
            }
        };
        Observable<List<T>> listObservable = Observable.just(searchContent)
                .map(new Func1<String, String>() {
                    @Override
                    public String call(String s) {
                        return s.toLowerCase();
                    }
                })
                .map(new Func1<String, List<T>>() {
                    @Override
                    public List<T> call(String lowerCaseSearch) {
                        List<T> searchResultEntityList = new ArrayList<>();

                        if (lowerCaseSearch.matches("[a-z]{1}")) {

                            String search = lowerCaseSearch.toUpperCase();
                            if (mAlphabetMap.containsKey(search)) {
                                searchResultEntityList.addAll(mAlphabetMap.get(search));
                            }

                        } else if (lowerCaseSearch.matches("[a-z]{2,3}")) {

                            for (T t : mSearchEntityList) {
                                String shortSpell = AlphabetHelper.converterToShortSpell(t.getDisplayContent());
                                if (shortSpell.contains(lowerCaseSearch)) {
                                    searchResultEntityList.add(t);
                                }
                            }

                        } else if (lowerCaseSearch.matches("[a-z]{4,}")) {

                            for (T t : mSearchEntityList) {
                                String fullSpell = AlphabetHelper.converterToFullSpell(t.getDisplayContent());
                                if (fullSpell.contains(lowerCaseSearch)) {
                                    searchResultEntityList.add(t);
                                }
                            }

                        } else {
                            for (T t : mSearchEntityList) {
                                String displayContent = t.getDisplayContent();
                                int length = searchContent.length();
                                if (length == 1) {
                                    String cityStr = String.valueOf(displayContent.charAt(0));
                                    if (cityStr.equals(searchContent)) {
                                        searchResultEntityList.add(t);
                                    }
                                } else if (length > 1) {
                                    if (displayContent.equals(searchContent)) {
                                        searchResultEntityList.add(t);
                                    }
                                }
                            }
                        }
                        return searchResultEntityList;
                    }
                })
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());
        subscription = listObservable.subscribe(action1);
    }


    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        subscription.unsubscribe();
    }

    @Override
    public void attachView(BaseView view) {
        if (view instanceof AlphabetSearchView) {
            mAlphabetSearchView = (AlphabetSearchView) view;
        }
    }
}
